package usergroup

import (
	"context"

	v1 "gitee.com/liangziyisheng/caas-platform-mc/api/usergroup/v1"
	"gitee.com/liangziyisheng/caas-platform-mc/internal/consts"
	"gitee.com/liangziyisheng/caas-platform-mc/internal/service"
)

func (c *ControllerV1) List(ctx context.Context, req *v1.ListReq) (res *v1.ListRes, err error) {
	tenantCode := ctx.Value(consts.TokenClaims.TenantCode).(string)

	in := &v1.ListInput{}
	in.Name = req.Name
	in.TenantCode = tenantCode
	in.Page = req.Page
	in.Size = req.Size
	out, err := service.UserGroup().List(ctx, in)
	if err != nil {
		return nil, err
	}
	res = &v1.ListRes{Total: out.Total, Data: make([]v1.UserGroup, 0)}
	for _, v := range out.Data {
		t := v1.UserGroup{
			Description: v.Description,
			Id:          v.Id,
			Name:        v.Name,
		}
		res.Data = append(res.Data, t)
	}
	return res, nil
}
